//
//  AmuseCell.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AmuseCell.h"

@interface AmuseCell ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation AmuseCell
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 40, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_tableView];
        [_tableView release];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // 页面与首页24小时要闻cell样式相同 用ImportNewsModel/ImportNewsCell/ImportThreePicCell
    [_tableView registerClass:[ImportNewsCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView registerClass:[ImportThreePicCell class] forCellReuseIdentifier:@"threeCell"];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
}
// 上拉加载
- (void)footerData
{
    static NSInteger t = 20;
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/T1348648517839/%ld-20.html", t];
   [AFHTTPHandler GETWithURL:str completion:^(id result) {
       NSArray *arr = [ImportNewsModel modelHandleWithArray:result[@"T1348648517839"]];
       for (NSInteger i = 0; i < arr.count; i++) {
           [_dataArr addObject:arr[i]];
       }
       [self.tableView reloadData];
   }];
    t += 20;
}

- (void)headerData
{    
    NSString *str = @"http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"T1348648517839"];
        self.dataArr = [ImportNewsModel modelHandleWithArray:arr];
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
 


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImportNewsModel *m = _dataArr[indexPath.row];
    if (m.imgextra.count == 0) {
        ImportNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.imgsrc sd_setImageWithURL:[NSURL URLWithString:m.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.title.text = m.title;
        cell.digest.text = m.digest;
        cell.replyCount.text = [[m.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell;
    }else{
        ImportThreePicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeCell" forIndexPath:indexPath];
        cell.title.text  =m.title;
        // 取imgextra数组中key为imgsrc对应的值
        // [cell.imgsrcOne sd_setImageWithURL:[NSURL URLWithString:m.imgextra[0] [@"imgsrc"]]];
        [cell.imgsrc sd_setImageWithURL:[NSURL URLWithString:m.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.imgsrcOne sd_setImageWithURL:[NSURL URLWithString:m.imgextra[0]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        [cell.imgsrcTwo sd_setImageWithURL:[NSURL URLWithString:m.imgextra[1]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.replyCount.text = [[m.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImportNewsModel *m = _dataArr[indexPath.row];
    if (m.imgextra.count == 0){
        return HEIGHT * 100;
    }else{
        return HEIGHT * 135;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImportNewsModel *m = _dataArr[indexPath.row];
    [self.delegate amuseyuleDelegate:m.url];

}

@end
